home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / trap13.arc / TRAP13.ASM < prev    next >
Assembly Source File  |  1988-04-09  |  10KB  |  355 lines

  1. name    Td
  2. page    63,110
  3.  
  4. CODE    segment
  5.         assume  CS:CODE
  6.  
  7. Td      proc    far
  8.         org     100h                ; .COM file
  9.  
  10. start:  jmp     instal              ; to install code
  11.  
  12. savint: dd      ?                   ; save old tick addr
  13.  
  14. doing:  dw      ?                   ; routine execute status
  15.  
  16. calmsg:  db    '*calling*  from: '
  17. rtmsg:   db    '*return* '
  18.  
  19. save:    dw    6 dup(?)             ; to store regs
  20.  
  21. saveCS:  dw    ?
  22. saveIP   dw    ?
  23.  
  24. ourst:   db    24 dup('stack   ')
  25. estack:  db    0
  26.  
  27. ostseg:  dw    ?                    ; to save caller's stack
  28. ostofs:  dw    ?
  29.  
  30. tdsig    dw    7777h
  31.          dw    8888h
  32.          dw    9999h
  33.          dw    0aaaah
  34.          dw    0deadh
  35.  
  36. tdres   proc  near
  37.  
  38.         push  ax
  39.  
  40.         push  bp                           ; get caller's address
  41.         mov   bp,sp
  42.         mov   ax,word ptr [bp+4]
  43.         mov   word ptr [saveIP], ax
  44.         mov   ax, word ptr [bp+6]
  45.         mov   word ptr [saveCS], ax
  46.         pop   bp
  47.         pop   ax
  48.  
  49.         push  ax
  50.         push  ds                          ; save environment
  51.         pushf
  52.  
  53.         push  cs                          ; our data seg
  54.         pop   ds
  55.  
  56.         cmp   byte ptr [doing],00h  ; can we execute?
  57.         jz    tdgk
  58.         jmp   tdext
  59.  
  60. tdgk:   mov   byte ptr [doing],0ffh    ; sure, we can execute
  61.  
  62.         mov   word ptr [save],ax       ; save more stuff
  63.         mov   word ptr [save+4],cx
  64.  
  65.  
  66.  
  67.         cli                            ; no interrupts
  68.         mov   ax,ss                    ;  during stack munch
  69.         mov   ostseg,ax
  70.         mov   ostofs,sp
  71.         mov   ax,ds                    ; point to our stack
  72.         mov   ss,ax
  73.         mov   sp,offset estack
  74.         sti
  75.  
  76.         call  sr                       ; calling message
  77.         mov   cx,17
  78.         mov   si, offset calmsg
  79.         call  pfc
  80.         mov   ax,word ptr[saveCS]
  81.         call  pword
  82.         mov   al,':'
  83.         mov   ah,0
  84.         int   17h
  85.         mov   ax,word ptr[saveIP]
  86.         call  pword
  87.         call  pcrlf
  88.  
  89.         call  dispr                      ; print caller's regs
  90. ;        call  pcrlf
  91.         call  rr
  92.  
  93.         mov   ax,word ptr[save]        ; get his args
  94.         mov   cx,word ptr[save+4]
  95.  
  96.         pushf
  97.         call dword ptr [savint]     ; simulate an OLD int
  98.  
  99.         mov  word ptr [save], ax       ; save return args
  100.         mov  word ptr [save+4],cx
  101.  
  102.         call sr                     ; save regs
  103.  
  104.         mov   cx,9                    ; print return regs
  105.         mov   si,offset rtmsg
  106.         call  pfc
  107.         call  pcrlf
  108.         call  dispr
  109.         call  pcrlf
  110.  
  111.         call  rr                   ; restore
  112.  
  113.         cli
  114.         mov   ax,ostseg            ; retrieve normal stack
  115.         mov   ss,ax
  116.         mov   sp,ostofs
  117.         sti
  118.  
  119.         mov   cx,word ptr [save+4]   ; cx too
  120.  
  121. tdext:  popf
  122.         pop   ds                     ; retreive environment
  123.         pop   ax
  124.  
  125.         mov   ax,word ptr [save+10]    ; return flags
  126.         push  ax
  127.         popf
  128.  
  129.         pop   ax                        ; junk caller's ip
  130.         pop   ax                        ; junk his CS
  131.         pop   ax                        ; finally his flags
  132.         pushf                           ; give him OURS
  133.         mov   ax, word ptr[saveCS]      ; and his CS back
  134.         push  ax
  135.         mov   ax, word ptr[saveIP]      ; and his IP
  136.         push  ax
  137.  
  138.         mov   ax, word ptr[save]        ; former INT13's AX result
  139.  
  140.         mov   byte ptr [doing],00h      ; no longer executing
  141.  
  142.         iret                            ; adios
  143.  
  144. ascii   db    '0123456789abcdef'
  145. regs    db    ' AX  BX  CX  DX  ES '
  146. Flags   db    '  Carry: '
  147.  
  148. dispr:  mov   si,0                        ; save regs in core
  149.         inc   si
  150.         inc   si
  151.         mov   word ptr [save+si],bx
  152.         inc   si
  153.         inc   si
  154.         inc   si
  155.         inc   si
  156.         mov   word ptr [save+si],dx
  157.         inc   si
  158.         inc   si
  159.         push  es
  160.         pop   ax
  161.         mov   word ptr [save+si],ax
  162.         inc   si
  163.         inc   si
  164.         pushf
  165.         pop   ax
  166.         and   ax,1
  167.         mov   word ptr [save+si],ax
  168.  
  169.         mov   cx,5                         ; 5 regs to print
  170.         cld
  171.  
  172.         mov   si, offset regs              ; point to names
  173.         mov   di, offset save              ; and values
  174.  
  175. dpl:    push  cx
  176.  
  177.         mov   cx,4                         ; name is 4 long
  178.         call  pfc                          ; print it
  179.  
  180.         mov   ax,word ptr [di]             ; next reg value
  181.         push  cx                           ; print value
  182.         call  pword
  183.         pop   cx
  184.  
  185.         inc   di                           ; next value
  186.         inc   di
  187.  
  188.         pop   cx                           ; restore counter
  189.         loop  dpl                          ; do it again
  190.  
  191.         mov   cx,9                         ; flags message
  192.         mov   si, offset flags
  193.         call  pfc
  194.         mov   ax,word ptr [di]
  195.         call  pword
  196.         call  pcrlf
  197.  
  198.         ret
  199.  
  200. pword:  mov   cl,8                         ; sloppy print hex word
  201.         ror   ax,cl                        ;  routine follows
  202.         mov   ch,ah
  203.         push  cx
  204.         call  pbyte
  205.         pop   cx
  206.         mov   ah,ch
  207.         ror   ax,cl
  208.         call  pbyte
  209.         ret
  210.  
  211. pbyte:  mov   cl,4
  212.         ror   al,cl
  213.         push  cx
  214.         call  phdig
  215.         pop   cx
  216.         ror   al,cl
  217.         call  phdig
  218.         ret
  219.  
  220. phdig:  mov   cl,al
  221.         and   al,0fh
  222.         mov   bx,offset ascii
  223.         xlat  ascii
  224.         mov   ah,0
  225.         int   17h
  226.         mov   al,cl
  227.         ret
  228.  
  229. pfc:    push  ax                         ; print a string pointed to
  230. plfc:   mov   ah,0                       ;  by SI, CX long
  231.         lodsb                            ;
  232.         int   17h
  233.         loop  plfc
  234.         pop   ax
  235.         ret
  236.  
  237. crlf:   db    0dh
  238.         db    0ah
  239.  
  240. pcrlf:  push  si                         ; obviously
  241.         push  cx
  242.         mov   cx,2
  243.         mov   si, offset crlf
  244.         call  pfc
  245.         pop   cx
  246.         pop   si
  247.         ret
  248.  
  249. sr:     pop   ax                         ; cool save regs routine
  250.  
  251.         pushf                            ; in real life, use :
  252.         push  bx                         ;
  253.         push  cx                         ;   push  ax
  254.         push  dx                         ;   call  sr
  255.         push  si                         ;     .
  256.         push  di                         ;     .
  257.         push  bp                         ;     .
  258.         push  es                         ;   call  rr
  259.         push  ds                         ;   pop   ax
  260.                                          ;
  261.         push  ax
  262.         ret
  263.  
  264. rr:     pop   ax
  265.  
  266.         pop   ds                    ; restore regs
  267.         pop   es
  268.         pop   bp
  269.         pop   di
  270.         pop   si
  271.         pop   dx
  272.         pop   cx
  273.         pop   bx
  274.         popf
  275.  
  276.         push ax
  277. tdend:  ret
  278.  
  279. tdall:  nop                              ; "offset + 1" of res
  280. tdres   endp
  281.  
  282. tdlen   equ     tdend-tdres         ; routine length
  283. siglen  equ     tdres-tdsig         ; check length
  284.  
  285. intoff  equ     (13h*4)+0             ; addr of tick vector
  286. intseg  equ     (13h*4)+2             ;
  287.  
  288. instal: push    ds                  ; save DOS return word
  289.         xor     ax,ax               ; clear out ax
  290.         push    ax                  ; for return
  291.         push    ax                  ; and to zero ES
  292.         pop     es
  293.         mov     ax,es:[intoff]      ; get tick vector offset
  294.         push    ax                  ; move into di
  295.         pop     di
  296.         sub     di,siglen           ;
  297.         mov     ax,es:[intseg]      ; get tick vector seg
  298.         push    ax                  ; move into es
  299.         pop     es                  ;
  300.         mov     si,offset tdsig     ; get our routine's address
  301.         cld                         ; forward compare
  302.         mov     cx,siglen           ; length of routine
  303.  
  304. rescmp: cmps    ds:byte ptr[si],es:[di] ; loop while Z
  305.         loopz   rescmp
  306.  
  307.         jz      reserr              ; if all equal, already res.
  308.  
  309.         xor     ax,ax               ; segment 0000
  310.         push    ax
  311.         pop     es
  312.         mov     ax,word ptr es:[intoff]      ; get tick vector offset
  313.         mov     bx,offset savint
  314.         mov     word ptr [bx],ax      ; store tick routine's offset
  315.  
  316.         mov     ax,word ptr es:[intseg]      ; get tick routine's segment
  317.         inc     bx
  318.         inc     bx
  319.         mov     word ptr [bx],ax             ; store
  320.  
  321.         mov     byte ptr [doing], 0ffh          ; fake executing
  322.  
  323.         cli                         ; no interrupts while
  324.  
  325.         mov     word ptr es:[intoff], offset tdres  ;  storing
  326.         mov     word ptr es:[intseg], cs            ;  vectors
  327.  
  328.         sti                         ; interrupts Again
  329.  
  330.         mov     dx,offset resmsg    ; good msg
  331.         mov     ah,9                ; output string
  332.         int     21h                 ; display
  333.  
  334.         mov     doing, 00h          ; allow executing
  335.  
  336.         mov     dx,tdall            ; our resident portion
  337.         int     27h                 ; terminate & stay resident
  338.  
  339. reserr: mov     dx,offset errmsg    ; error msg
  340.         mov     ah,9                ; output string
  341.         int     21h                 ; display
  342.  
  343.         int     20h                 ; go home
  344.  
  345. Td      endp
  346.  
  347. resmsg: db      'TRAP13 - (c) 1985 Eric Pederson - Resident portion loaded'
  348.         db      '$'
  349.  
  350. errmsg: db      'TRAP13 - Already resident'
  351.         db      '$'
  352.  
  353. CODE    ends
  354.         end     start
  355.